package com.chookss.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.tools.FileUtilss;
import com.chookss.tools.Utils;

import java.io.File;

/**
 * 水印
 * <pre>
 *     author  : Fantasy
 *     version : 1.0, 2019-07-29
 *     since   : 1.0, 2019-07-29
 * </pre>
 */
public class Watermark {
    /**
     * 水印文本
     */
    private String mText;
    /**
     * 字体颜色，十六进制形式，例如：0xAEAEAEAE
     */
    private int mTextColor;
    /**
     * 字体大小，单位为sp
     */
    private float mTextSize;
    /**
     * 旋转角度
     */
    private float mRotation;
    private static Watermark sInstance;
    private static Activity mActivity;

    private Watermark() {
        mText = "";
        mTextColor = 0xAEAEAEAE;
        mTextSize = 18;
        mRotation = -25;
    }

    //不能用单例，这样全局就一个了，实际是重点页面都要有
//    public static Watermark getInstance(Activity activity) {
//        if (sInstance == null) {
//            synchronized (Watermark.class) {
//                sInstance = new Watermark();
//                mActivity = activity;
//            }
//        }
//        return sInstance;
//    }
    public static Watermark getInstance(Activity activity) {
        sInstance = new Watermark();
        mActivity = activity;
        return sInstance;
    }

    /**
     * 设置水印文本
     *
     * @param text 文本
     * @return Watermark实例
     */
    public Watermark setText(String text) {
        if (Utils.isNull(text)) {
            mText = mActivity.getResources().getString(R.string.app_name);
        } else {
            mText = text;
        }
        return sInstance;
    }


    /**
     * 设置字体颜色
     *
     * @param color 颜色，十六进制形式，例如：0xAEAEAEAE
     * @return Watermark实例
     */
    public Watermark setTextColor(int color) {
        mTextColor = color;
        return sInstance;
    }

    /**
     * 设置字体大小
     *
     * @param size 大小，单位为sp
     * @return Watermark实例
     */
    public Watermark setTextSize(float size) {
        mTextSize = size;
        return sInstance;
    }

    private String companyName;

    /**
     * 设置字体大小
     *
     * @return Watermark实例
     */
    public Watermark setCompanyName(String name) {

        if (Utils.isNull(name)) {
            companyName = mActivity.getResources().getString(R.string.app_name);
        } else {
            companyName = name;
        }
        return sInstance;
    }

    /**
     * 设置旋转角度
     *
     * @param degrees 度数
     * @return Watermark实例
     */
    public Watermark setRotation(float degrees) {
        mRotation = degrees;
        return sInstance;
    }

    /**
     * 显示水印，铺满整个页面
     */
    public void show() {
        show(mText, companyName, false);
    }

    public void show(boolean isFull) {
        show(mText, companyName, isFull);
    }

    /**
     * 显示水印，铺满整个页面
     *
     * @param text 水印
     */
    public void show(String text, String companyName, boolean isFull) {
        WatermarkDrawable drawable = new WatermarkDrawable(mActivity, companyName);
        drawable.mText = text;
        drawable.mTextColor = mTextColor;
        drawable.mTextSize = mTextSize;
        drawable.mRotation = mRotation;
        ViewGroup rootView = mActivity.findViewById(android.R.id.content);
        FrameLayout layout = new FrameLayout(mActivity);
        if (isFull) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            params.setMargins(0, Utils.dip2px(mActivity, 50) + Utils.getStatusBarHeight(mActivity), 0, Utils.dip2px(mActivity, 50));
            layout.setLayoutParams(params);
        }
        layout.setBackground(drawable);
        rootView.addView(layout);
    }

    private Bitmap mBitmap;

    private class WatermarkDrawable extends Drawable {
        private Paint mPaint;
        private Paint mPaintColor;
        private Paint mPaintColor2;
        private Paint mPaintLine;
        /**
         * 水印文本
         */
        private String mText;
        private String companyName;
        /**
         * 字体颜色，十六进制形式，例如：0xAEAEAEAE
         */
        private int mTextColor;
        /**
         * 字体大小，单位为sp
         */
        private float mTextSize;
        private Context mContext;
        /**
         * 旋转角度
         */
        private float mRotation;

        private WatermarkDrawable() {
            mPaint = new Paint();
        }

        private WatermarkDrawable(Activity activity, String name) {
            mContext = activity;
            companyName = name;
            mPaint = new Paint();
            mPaintColor = new Paint();
            mPaintColor2 = new Paint();
            mPaintLine = new Paint();
            mBitmap = null;
        }

        @Override
        public void draw(@NonNull Canvas canvas) {

            int width = getBounds().right;
            int height = getBounds().bottom;
            int diagonal = (int) Math.sqrt(width * width + height * height); // 对角线的长度
            mPaintColor.setTextSize(mTextSize); // ConvertUtils.spToPx()这个方法是将sp转换成px，ConvertUtils这个工具类在我提供的demo里面有
            mPaintColor.setAntiAlias(true);
            mPaintColor2.setTextSize(mTextSize); // ConvertUtils.spToPx()这个方法是将sp转换成px，ConvertUtils这个工具类在我提供的demo里面有
            mPaintColor2.setAntiAlias(true);
            mPaintLine.setColor(mTextColor);
            mPaintLine.setStrokeWidth((float) Utils.dip2px(mContext, (float) 1));
            //白底
            if (mBitmap == null) {
                mBitmap = FileUtilss.getDiskBitmap(mContext, mContext.getExternalFilesDir(Urls.IMAGEFOLDER).getAbsolutePath() + File.separator + "watermarkBlack.jpg");
//                    mBitmap = ((BitmapDrawable) mContext.getResources().getDrawable(R.drawable.logo_sy_black)).getBitmap();
            }

            mPaintColor.setColor(mTextColor);
            mPaintColor2.setColor(mTextColor);
            canvas.drawColor(0x00000000);
            if (Utils.isNull(mText)) {
                mText = mActivity.getResources().getString(R.string.app_name);
            }
            if (Utils.isNull(companyName)) {
                companyName = mActivity.getResources().getString(R.string.app_name);
            }
            float textWidth = mPaintColor.measureText(mText);
            canvas.rotate(mRotation);
            mPaintColor.setTextAlign(Paint.Align.CENTER);
//            mPaintColor2.setTextAlign(Paint.Align.CENTER);
            int index = 0;
            int space = Utils.dip2px(mContext, 2);
            float fromX;
            int companyNameWidth = (int) (mTextSize * companyName.length());
            // 以对角线的长度来做高度，这样可以保证竖屏和横屏整个屏幕都能布满水印
            for (int i = 0; i < 2; i++) {
                int positionX = 0, positionY = 0;
                if (i == 0) {
                    positionY = diagonal / 4;
                    positionX = (int) (-textWidth); // 上下两行的X轴起始点不一样，错开显示
                } else {
                    positionY = 2 * diagonal / 3;
                    positionX = (int) (-width + textWidth*2/3 ); // 上下两行的X轴起始点不一样，错开显示
                }
                if (mBitmap == null) {
                    canvas.drawText(companyName, positionX, positionY, mPaintColor2);
                    canvas.drawLine(positionX - mTextSize, positionY + mTextSize * 2 / 3, positionX + companyNameWidth + mTextSize, positionY + mTextSize * 2 / 3, mPaintLine);
                    canvas.drawText(mText, positionX + companyNameWidth / 2, positionY + mTextSize * 2, mPaintColor);
                } else {
                    canvas.drawBitmap(mBitmap, positionX, positionY, mPaint);
                    canvas.drawText(companyName, positionX + mBitmap.getWidth(), positionY + mBitmap.getHeight() * 3 / 4 + space, mPaintColor2);
                    canvas.drawLine(positionX - mTextSize / 2, positionY + mBitmap.getHeight() * 7 / 5, positionX + mBitmap.getWidth() + companyNameWidth + mTextSize / 2, positionY + mBitmap.getHeight() * 7 / 5, mPaintLine);
                    canvas.drawText(mText, positionX + (mBitmap.getWidth() + companyNameWidth + space) / 2, positionY + mBitmap.getHeight() * 13 / 5, mPaintColor);
                }
            }
//            for (int positionY = diagonal / 6; positionY <= diagonal; positionY += diagonal / 6) {
//                fromX = -width + (index++ % 2) * textWidth; // 上下两行的X轴起始点不一样，错开显示
//                for (float positionX = fromX; positionX < width; positionX += textWidth * 3) {
//                    if (mBitmap == null) {
//                        canvas.drawText(companyName, positionX, positionY, mPaintColor2);
//                        canvas.drawLine(positionX- mTextSize, positionY + mTextSize*2/3, positionX+companyNameWidth+ mTextSize, positionY + mTextSize*2/3, mPaintLine);
//                        canvas.drawText(mText, positionX + companyNameWidth/ 2, positionY + mTextSize*2, mPaintColor);
//                    } else {
//                        canvas.drawBitmap(mBitmap, positionX, positionY, mPaint);
//                        canvas.drawText(companyName, positionX+mBitmap.getWidth(), positionY + mBitmap.getHeight()*3/4+space, mPaintColor2);
//                        canvas.drawLine(positionX- mTextSize/2, positionY +mBitmap.getHeight()*7/5, positionX+mBitmap.getWidth()+companyNameWidth+ mTextSize/2, positionY +mBitmap.getHeight()*7/5, mPaintLine);
//                        canvas.drawText(mText, positionX + (mBitmap.getWidth() +companyNameWidth+space)/ 2, positionY + mBitmap.getHeight()*13/5, mPaintColor);
//
//                    }
//                }
//            }

            canvas.save();
            canvas.restore();
        }

        @Override
        public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
        }

        @Override
        public void setColorFilter(@Nullable ColorFilter colorFilter) {
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

    }


}